// $Id: loggerrequestfail.cpp 65 2004-10-27 21:44:50Z hansemil $
#include "loggerrequestfail.h"
#include "loghandler.h"
#include "node.h"
#include "noderequestfileparameters.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;

/**
 * Init members and register apropriate listeners at the log handler.
 */
LoggerRequestFail::LoggerRequestFail()
	: 		m_countRequestFail(0), m_countRequestSuccess(0), m_countInsertFail(0), m_countInsertSuccess(0)
{
	LogHandler::getInstance()->addLogger(this, "clock_tick");
	LogHandler::getInstance()->addLogger(this, "node_requestFile");
}

/**
 * Opens file for writing and stores the Eos pointer
 */
void LoggerRequestFail::eos_simulate_start(void *eos)
{
	openFile(&m_fout, "requestfail.csv");
	m_fout << "Time Step, Request Success Count, Request Fail Count, Insert Success Count, Insert Fail Count" << endl;

	m_eos = (Eos *)eos;
}

/**
 * Closes file
 */
void LoggerRequestFail::eos_simulate_stop(void *eos)
{
	m_fout.close();
}

/**
 * Writes the statistics for each tick
 */
void LoggerRequestFail::clock_tick(unsigned int time)
{
	// Write time step
	m_fout << time;

	// Write statistics
	m_fout << "," << m_countRequestSuccess << "," << m_countRequestFail << "," << m_countInsertSuccess << "," << m_countInsertFail;

	// Write end
	m_fout << endl;

	// Clear variables for next timestep
	m_countRequestSuccess = 0;
	m_countRequestFail = 0;
	m_countInsertSuccess = 0;
	m_countInsertFail = 0;
}

/**
 * Checks if a request or insert call is a final success or final fail and saves it for writing on tick
 */
void LoggerRequestFail::node_requestFile(void *thisNode, void *params_in, bool result)
{
	NodeRequestFileParameters *params = (NodeRequestFileParameters *)params_in;

	// Check if it's an insert
	if(!params->m_insert){
		if(result){
			m_countRequestSuccess++;
		}
		else{
			m_countRequestFail++;
		}
	}
	else{
		if(!result){
			m_countInsertSuccess++;
		}
		else{
			m_countInsertFail++;
		}
	}
}
